﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS
{
    public partial class Ins_Examination : System.Web.UI.Page
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;
        string id;

        protected void Page_Load(object sender, EventArgs e)
        {
            ((Menu)Master.FindControl("Menu1")).Items[2].Selected = true;

            DateCalendar.SelectedDate = DateTime.Today;
        }

        protected void add_clicked(object sender, EventArgs e)
        {
            // Connect to the Database
            conn = new MySqlConnection(strProvider);
            conn.Open();

            string id = (string)Session["userName"];

            if(id == null)
                Response.Redirect("../login.aspx");
            try 
            {
                string BeginTimeString = DateCalendar.SelectedDate.Year + "-" + DateCalendar.SelectedDate.Month + "-" + DateCalendar.SelectedDate.Day + " " +
                                         BeginTimeH.Text + ":" + BeginTimeM.Text + ":00";

                string EndTimeString = DateCalendar.SelectedDate.Year + "-" + DateCalendar.SelectedDate.Month + "-" + DateCalendar.SelectedDate.Day + " " +
                                        EndTimeH.Text + ":" + EndTimeM.Text + ":00";

                string strSQL = "INSERT INTO Examination(e_startTime, e_endTime, e_type) " +
                                " VALUES('" + BeginTimeString + "', '" + EndTimeString + "', '" + TypeList.SelectedValue + "' )";
                sqlCommand = new MySqlCommand(strSQL, conn);
                sqlCommand.ExecuteNonQuery();

            }
            catch (MySqlException ex)
            {
                string msg = "Insert Error:";
                msg += ex.Message;
                ErrorLabel.Text = msg;
                ErrorLabel.Visible = true;
                goto end;
            }

            ErrorLabel.Text = "Successfully Added!";

            end:
            conn.Close();
        }
    }
}